package com.yunkaweilai.android.activity.member;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.classic.common.MultipleStatusView;
import com.google.gson.Gson;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.base.BaseActivity;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.model.member.PercentagesStaffModel;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.TitleBuilder;
import com.yunkaweilai.android.utils.Utils;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 提成员工
 * Created by Administrator on 2017/7/3.
 */

public class PercentagesStaffActivity extends BaseActivity {

    @BindView(R.id.content_view)
    ListView contentView;
    @BindView(R.id.id_multipleStatusView)
    MultipleStatusView idMultipleStatusView;
    private CommonAdapter<PercentagesStaffModel.DataBean.ListBean> adapter;
    private ArrayList<PercentagesStaffModel.DataBean.ListBean> list = new ArrayList<>();
    public static final String PERCENTAGES_HTTP_URL = "PERCENTAGES_HTTP_URL";
    private String url = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_percentages_staff);
        ButterKnife.bind(this);
        url = getIntent().getStringExtra(PERCENTAGES_HTTP_URL);
        idMultipleStatusView.showLoading();
        idMultipleStatusView.setOnRetryClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initHttp();
            }
        });
        new TitleBuilder(mAc).setLeftImage(R.mipmap.ic_go_back).setTitleText("提成员工")
                .setLeftOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });

        initAdapter();
        initClick();
        initHttp();
    }

    private void initClick() {
        contentView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.putExtra(MemberRechargeActivity.PERCENTAGES_STAFF_NAME,list.get(position).getEmployee_name());
                intent.putExtra(MemberRechargeActivity.PERCENTAGES_STAFF_ID,list.get(position).getId());
                setResult(RESULT_OK,intent);
                finish();
            }
        });
    }

    private void initAdapter() {
        adapter = new CommonAdapter<PercentagesStaffModel.DataBean.ListBean>(mAc,R.layout.item_list_percentags_staff,list) {
            @Override
            protected void convert(ViewHolder viewHolder, PercentagesStaffModel.DataBean.ListBean item, int position) {
                viewHolder.setText(R.id.id_tv_percentags_name,item.getEmployee_name());
            }
        };
        contentView.setAdapter(adapter);
    }

    private void initHttp() {
        HttpRequest.postUrl(url)
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(mAc, json);
                        if (checkCode){
                            Gson gson = new Gson();
                            list.clear();
                            PercentagesStaffModel percentagesStaffModel = gson.fromJson(json, PercentagesStaffModel.class);
                            PercentagesStaffModel.DataBean.ListBean listBean = new PercentagesStaffModel.DataBean.ListBean();
                            listBean.setId("0");
                            listBean.setEmployee_name("无");
                            listBean.setStation_name("");
                            list.add(listBean);
                            list.addAll(percentagesStaffModel.getData().getList());
                            adapter.notifyDataSetChanged();
                            if (idMultipleStatusView != null) {
                                idMultipleStatusView.showContent();
                            }
                        }else{
                            if (idMultipleStatusView != null) {
                                idMultipleStatusView.showError();
                            }
                        }
                    }

                    @Override
                    public void onError(Exception e) {
                        if (idMultipleStatusView != null) {
                            idMultipleStatusView.showError();
                        }
                    }
                });
    }
}
